﻿Namespace GroupWindow

    Module mGroup

        Private WithEvents GroupWin As GUI.MirWindow
        Private isVisible As Boolean

        Private WithEvents btnBlockGrp As GUI.MirButton
        Private WithEvents btnPermitGrp As GUI.MirButton
        Private WithEvents btnAddToGrp As GUI.MirButton
        Private WithEvents btnRemoveFromGrp As GUI.MirButton
        Private WithEvents btnFormGrp As GUI.MirButton
        Public WithEvents btnClose As GUI.MirButton

        Public AllowGrp As Boolean
        Public aNames As New Collection
        Public GroupID As Long
        Public aGroupCharIDs As New Collection

        Public Sub SetGrpID(struct As GroupID)

            GroupID = struct.ID

            If GroupID = 0 Then
                aNames = New Collection
                aGroupCharIDs = new Collection
            End If

        End Sub

        Public Sub AddGrpPkt(struct As GroupNameID)

            aNames.Add(struct.Name, struct.ID.ToString)
            aGroupCharIDs.Add(struct.ID, struct.ID.ToString)

        End Sub

        Public Sub RemoveGrpPkt(struct As GroupID)

            aNames.Remove(struct.ID.ToString)
            aGroupCharIDs.Remove(struct.ID.ToString)

        End Sub

        Public Sub FormGrp(name As String)

            Dim pkt As Packet
            Dim struct As GroupName

            pkt.PacketType = GamePacketType.FormGroup
            pkt.PacketOpCode = PacketOpCode.Enquiry
            pkt.Username = Globals.sUsername

            struct.Name = name

            ClientSock.SendData(MakePacket(pkt, struct, globals.Crypto))

        End Sub

        Public Sub AddToGroup(name As String)

            Dim pkt As Packet
            Dim struct As GroupName

            pkt.PacketType = GamePacketType.AddToGrp
            pkt.PacketOpCode = PacketOpCode.Enquiry
            pkt.Username = Globals.sUsername

            struct.Name = name

            ClientSock.SendData(MakePacket(pkt, struct, Globals.Crypto))

        End Sub

        Public Sub RemoveFromGroup(name As String)

            Dim pkt As Packet
            Dim struct As GroupName

            pkt.PacketType = GamePacketType.RemoveFrmGrp
            pkt.PacketOpCode = PacketOpCode.Enquiry
            pkt.Username = Globals.sUsername

            struct.Name = name

            ClientSock.SendData(MakePacket(pkt, struct, Globals.Crypto))

        End Sub

        Public Sub Init()

            GroupWin = New GUI.MirWindow("GroupWindow", 200, 200, 245, 248, True)

            btnBlockGrp = New GUI.MirButton(GroupWin, "btnBlockGrp", 183, 17, 0.0F, ImgLibs.GameInter, 178, 178, 178, "Block Group")
            btnPermitGrp = New GUI.MirButton(GroupWin, "btnPermitGrp", 183, 17, 0.0F, ImgLibs.GameInter, 179, 179, 179, "Permit Group")
            btnAddToGrp = New GUI.MirButton(GroupWin, "btnAddToGrp", 29, 191, 0.0F, ImgLibs.GameInter, 0, 173, 173, "Add")
            btnRemoveFromGrp = New GUI.MirButton(GroupWin, "btnRemoveFromGrp", 79, 191, 0.0F, ImgLibs.GameInter, 0, 175, 175, "Remove")
            btnFormGrp = New GUI.MirButton(GroupWin, "btnFormGrp", 129, 191, 0.0F, ImgLibs.GameInter, 0, 177, 177, "Group Formation")
            btnClose = New GUI.MirButton(GroupWin, "btnClose", 195, 196, 0.0F, ImgLibs.GameInter, 3036, 3036, 3036, "Close")

            AllowGrp = Globals.iniConfig.GetBoolean("INGAME", "ALLOWGRP", True)

            btnBlockGrp.Visible = AllowGrp
            btnPermitGrp.Visible = Not AllowGrp

        End Sub

        Public Sub Show()

            If Not isVisible Then

                Windows.AddWindow(GroupWin, Windows.WindowType.AlwaysOnTop)
                isVisible = True

            End If

        End Sub

        Public Sub Hide()

            If isVisible Then

                Windows.RemoveWindow(GroupWin)
                isVisible = False

            End If

        End Sub

        Public Sub Shutdown()

            Globals.iniConfig.WriteBoolean("INGAME", "ALLOWGRP", AllowGrp)

            Hide()

        End Sub

        Public Sub MsgBoxResultCallback(ByRef result As MessageBoxes.MsgBoxResult)

            Select Case result.mName

                Case "FORMGRP"
                    If result.mInput.Length > 0 Then

                        FormGroupWith(result.mInput)
                    End If

            End Select

        End Sub

        Private Sub GroupWin_DrawWindow(ByRef mode As Globals.DrawingMode, ByRef x As Integer, ByRef y As Integer, ByRef z As Single) Handles GroupWin.DrawWindow

            If mode = Globals.DrawingMode.Normal Then

                With Libs.GameInter

                    .Image(171).Draw(x - 8, y - 8, z)

                End With

            ElseIf mode = Globals.DrawingMode.Text Then

                DirectX.ProFont9.DrawNormalText(FormatGroupNames(aNames), x + 17, y + 63, Color.White, z)
                DirectX.ProFont9.DrawNormalText(Player.PlayerName, x + 101 - (DirectX.ProFont9.GetNormalTextWidth(Player.PlayerName) / 2), y + 26, Color.White, z)

            End If

        End Sub

        Private Function FormatGroupNames(names As Collection) As String

            Dim sOut As String = ""
            Dim lineCount As Integer = 1
            Dim tmpname As String

            tmpname = Player.PlayerName
            If tmpname.Length > 16 Then
                tmpname = Left(tmpname, 14) & ".."
            End If
            sOut &= tmpname
            sOut &= Space(18 - tmpname.Length)

            For Each name As String In names

                If lineCount = 2 Then
                    sOut &= vbCrLf
                    lineCount = 0
                End If

                tmpname = name
                If tmpname.Length > 16 Then
                    tmpname = Left(tmpname, 14) & ".."
                End If

                sOut &= tmpname

                lineCount += 1

                If lineCount = 1 Then sOut &= Space(18 - tmpname.Length)

            Next

            Return sOut

        End Function

        Private Sub ToggleAllowGrp()
            AllowGrp = Not AllowGrp
            btnBlockGrp.Visible = AllowGrp
            btnPermitGrp.Visible = Not AllowGrp
        End Sub

        Public Sub FormGroupWith(ByRef name As String)

            Dim p As Packet
            p.PacketType = GamePacketType.FormGroup

            'TODO: complete forming group


        End Sub

        Private Sub btnAddToGrp_Click() Handles btnAddToGrp.Click



        End Sub




        Private Sub btnAddToGrp_RequestingTooltip(tttxt As String) Handles btnAddToGrp.RequestingTooltip
            GameWindow.SetTooltip(tttxt, 0, True)
        End Sub

        Private Sub btnPermitGrp_Click() Handles btnPermitGrp.Click
            ToggleAllowGrp()
        End Sub

        Private Sub btnDisbandGrp_RequestingTooltip(tttxt As String) Handles btnPermitGrp.RequestingTooltip
            GameWindow.SetTooltip(tttxt, 0, True)
        End Sub

        Private Sub btnBlockGrp_Click() Handles btnBlockGrp.Click
            ToggleAllowGrp()
        End Sub

        Private Sub btnFormGrp_RequestingTooltip(tttxt As String) Handles btnBlockGrp.RequestingTooltip
            GameWindow.SetTooltip(tttxt, 0, True)
        End Sub

        Private Sub btnFormGrp_Click() Handles btnFormGrp.Click

            If GroupID = 0 Then

                MessageBoxes.AddMessageBox("FORMGRP", "Type a character name to form a group with", MessageBoxes.MsgBoxButtons.Input, AddressOf Game.MsgBoxResultCallback, Protocol.MAX_CHARNAME_LEN, , Protocol.VALID_CHARNAME_CHARS)

            Else

                MessageBoxes.AddMessageBox("INGRP", "Already in a group", MessageBoxes.MsgBoxButtons.OK, AddressOf Game.MsgBoxResultCallback)

            End If
        End Sub

        Private Sub btnLeaveGrp_RequestingTooltip(tttxt As String) Handles btnFormGrp.RequestingTooltip
            GameWindow.SetTooltip(tttxt, 0, True)
        End Sub

        Private Sub btnRemoveFromGrp_RequestingTooltip(tttxt As String) Handles btnRemoveFromGrp.RequestingTooltip
            GameWindow.SetTooltip(tttxt, 0, True)
        End Sub

        Private Sub btnClose_Click() Handles btnClose.Click
            Windows.RemoveWindow(GroupWin)
        End Sub
    End Module

End Namespace
